package org.ntu.csn.as.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ntu.csn.as.manager.DBManager;
import org.ntu.csn.as.model.Category;
import org.ntu.csn.as.model.SysMessage;
import org.ntu.csn.as.tool.Constant;

/**
 * Servlet implementation class TopicAddController
 */
@WebServlet("/TopicAddController")
public class TopicAddController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TopicAddController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Topic Add Controller ......");
		String action = request.getParameter("action").trim();
		if ("update".equals(action)) {
			update(request, response);

		} else {

			add(request, response);

		}
	}

	protected void update(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Topic Add Controller - Update");
		try {
		Category c = new Category();
		c.setGrade(Integer.parseInt(request.getParameter("grade")));
		c.setTopic(request.getParameter("topic"));
		c.setId(Integer.parseInt(request.getParameter("id")));

		DBManager dbm = new DBManager();
	
			dbm.updateCategory(c);
		} catch (Exception e) {
			SysMessage message = new SysMessage();
			message.setType(Constant.MESSAGE_ERROR);
			message.setMessage("Invalid Input, Please re-check your input");
			message.setSourceURL("TopicListController");
			request.setAttribute("errorMsg", message);
			e.printStackTrace();
			
		}

		request.getRequestDispatcher("TopicListController").forward(request,
				response);

	}

	protected void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Topic Add Controller - Add");
		try {
			Category c = new Category();
			c.setGrade(Integer.parseInt(request.getParameter("grade")));
			c.setTopic(request.getParameter("topic"));

			DBManager dbm = new DBManager();

			dbm.addCategory(c);
		} catch (Exception e) {
			SysMessage message = new SysMessage();
			message.setType(Constant.MESSAGE_ERROR);
			message.setMessage("Invalid Input, Please re-check your input");
			message.setSourceURL("TopicListController");
			request.setAttribute("errorMsg", message);
			e.printStackTrace();
		}

		request.getRequestDispatcher("TopicListController").forward(request,
				response);

	}

}
